﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VirtualDisk
{
    class DirCommand : Command
    {
        private MyString[] arguments = null;

        protected override int run()
        {
            MyString path;//要显示的目录路径
            MyString param;//参数
            processArgument(out path, out param);
            if (!DiskTree.getInstance().isExist(path))
            {
                return 1;
            }

            if (param == new MyString("/s"))
            {
                //递归显示目录和所有子目录信息
                DiskTree.getInstance().showAllNodeInfo(path, DiskNode.ShowOptions.DircoryAndFile);
            }
            else if (param == new MyString("/ad"))
            {
               //只显示目录
                DiskTree.getInstance().showNodeInfo(path, DiskNode.ShowOptions.OnlyDircory);
            }
            else if (MyString.IsNullOrEmpty(param))
            {
                //显示目录信息
                DiskTree.getInstance().showNodeInfo(path, DiskNode.ShowOptions.DircoryAndFile);
            }
            else
            {
                return 2;
            }

            return 0;
        }

        private void processArgument(out MyString path, out MyString param)
        {
            path = DiskTree.getInstance().getPWDPath();
            param = MyString.Empty;
            if (arguments.Length == 1)
            {
                if (arguments[0][0] != '/')
                    path = arguments[0].Trim();
                else
                    param = arguments[0].Trim();
            }

            if (arguments.Length == 2)
            {
                if (arguments[0][0] == '/')
                {
                    param = arguments[0].Trim();
                    path = arguments[1].Trim();
                }
                else
                {
                    path = (arguments[0] + arguments[1]).Trim();
                }

            }
        }

        protected override void processError(int returnCode)
        {
            switch (returnCode)
            {
                case 1:
                    Console.WriteLine("找不到文件！");
                    break;
                case 2:
                    Console.WriteLine("无效参数！");
                    break;
                default:
                    break;
            }
        }

        public DirCommand(MyString[] argu)
        {
            if (argu != null)
            {
                arguments = new MyString[argu.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    arguments[i] = argu[i];
                }
            }
        }

        
    }
}
